define(function(require, exports, module) {
  var $ = require("lib/jquery");
  var linkhash = {};
  
  exports.rooturl = "";
  exports.isChrome = isChrome = location.href.indexOf("loader")>0;
  
  exports.loadPage=function(url,success){
    $.get(exports.rooturl+url,function(result){
      $("body").append(result);
      if(success)
        success();
    });  
  };
  
  exports.playAudio = function(src) {
    my_media = null;// 音频播放器
    // 播放音频
    if(isChrome)
      return;
          
    function onSuccess() {
      //log("playAudio():Audio Success");
    }
    function onError(error) {
      //log('code: '    + error.code    + ' ' + error.message );
    }
    if(my_media!=null){
      my_media.release();
    }
    my_media = new Media(src, onSuccess, onError);
    my_media.play();
  };
  
  exports.readWord = function(word){
    exports.playAudio("http://dict.youdao.com/dictvoice?audio="+word);
  };
  
  exports.showMessage = function(text){
      //$.mobile.hidePageLoadingMsg();
      $.mobile.showPageLoadingMsg("a",text,true);
      setTimeout("$.mobile.hidePageLoadingMsg()",1000);
  };
  
  exports.getHashCode = function(str,caseSensitive){
      if(!caseSensitive){
          str = str.toLowerCase();
      }
      // 1315423911=b'1001110011001111100011010100111'
      var hash  =   1315423911,i,ch;
      for (i = str.length - 1; i >= 0; i--) {
          ch = str.charCodeAt(i);
          hash ^= ((hash << 5) + ch + (hash >> 2));
      }

      return  (hash & 0x7FFFFFFF).toString();
  };
  
  exports.pageHash = function(href){
    return "page"+exports.getHashCode(href);
  };
  
  exports.changePage = function($link){
    var href = $link.attr('href');
    var hash = exports.pageHash(href);
    
    if(linkhash[hash]){
      $.mobile.changePage("#"+hash);
    }else{
      var moduleName = $link.attr('module');//处理模块
      if(typeof(moduleName)=='undefined'){
        moduleName='html';
      }
      require.async(moduleName,function(module){
        module.onLink($link);
      });
    }
  };
  
  exports.regPage = function(href){
    linkhash[exports.pageHash(href)]=href;
  };
  
  exports.makeWord = function ($html){
  //给单词加回调
    $html.find(":not(iframe)").andSelf().contents().filter(function() {
        return this.nodeType == 3;
    }).each(function(){
      text = this.nodeValue.replace(/([\w'-]+)/g,"<span class='word'>$1</span>");          
      $(this).replaceWith(text);
    });    
  }
  
});